package com.atguigu.gulimall.ware.listener;

import com.atguigu.common.to.OrderTo;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.LockStockRabbitMqVo;
import com.atguigu.gulimall.ware.vo.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "stock.release.stock.queue")
public class LockStockListener {
    @Autowired
    private WareSkuService wareSkuService;
    @RabbitHandler
    public void unLock(LockStockRabbitMqVo lockStockRabbitMqVo, Message message,Channel channel) throws IOException {
        System.out.println("=====================收到解锁库存的消息=====================");
        try {
            wareSkuService.unLock(lockStockRabbitMqVo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @RabbitHandler
    public void unLock(OrderTo orderTo, Message message, Channel channel) throws IOException {
        System.out.println("=====================订单取消收到解锁消息=====================");
        try {
            wareSkuService.unLock(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
